import * as Cesium from 'cesium';

class AirMoveLineClass {
  constructor(viewer) {
    this.viewer = viewer;
    this.applyAirLineEntArr = []
    this.applyAirLineGrid = []
    let dirArrX = ['x', 'xy', 'xz', 'xyz', '-x', '-xy', '-xz', '-xyz']
    let dirArrY = ['y', 'xy', 'yz', 'xyz', '-y', 'x-y', '-yz', 'x-yz']
    let dirArrZ = ['z', 'xz', 'yz', 'xyz', '-z', 'x-z', 'y-z', 'xy-z']
    this.dirArr = [...new Set([...dirArrX, ...dirArrY, ...dirArrZ])];

    this.uavid = ''
    this.origin = [117.1213838888, 40.1407018888, 20000]
    this.gSize = [0.0001, 0.0001, 15]
    this.flyRun = [0.00001, 1000]
    this.llaLineArr = []
    this.GridTrackArr = []
    this.GridEntitiesList = [[], [], []]
    this.nextDirection = '0_0_0'
    this.flexBox = []
  }
  initLonLatAltGrid () {
    this.llaLineArr = []
    this.GridTrackArr = []
    this.GridEntitiesList = [[], [], []]
    this.nextDirection = '0_0_0'
    this.flexBox = []
    this.GridEntitiesList.forEach(type => {
      type.forEach(box => {
        this.viewer.entities.remove(box);
      });
    });
  }
  // 稀疏轨迹间隔计算（补点）
  supplementPos (pos4) {
    let faceArr = []
    let posNumX = (pos4[0][0] - pos4[1][0]) / (this.gSize[0] * 2)
    let posNumY = (pos4[0][1] - pos4[3][1]) / (this.gSize[1] * 1.5)
    console.log(posNumX);
  }
  addAirLine (posLits) {
    if (posLits.length > 0) {
      const linePosXYZArr = Cesium.Cartesian3.fromDegreesArrayHeights(posLits.flat())
      this.applyAirLineEntArr.push(this.viewer.entities.add({ polyline: { positions: linePosXYZArr, width: 5, material: Cesium.Color.GREEN } }))
      this.llaLineArr.forEach((element, index) => {
        // 白色
        this.testPos(Cesium.Cartesian3.fromDegrees(element[0], element[1], element[2]), index, 'WHITE')
      });
    }
  }
  drawOnePlane (pos4, posIndex) {
    const dop = this.viewer.entities.add({
      polygon: {
        hierarchy: Cesium.Cartesian3.fromDegreesArrayHeights(pos4.flat()),
        material: Cesium.Color.GREEN.withAlpha(0.1),
        perPositionHeight: true,
        outline: true,
      }
    })
    dop.name = `AirOnePlane_${posIndex}`
    dop.className = 'AirOnePlane'
    this.GridEntitiesList[2].push(dop)
  }
  // 世界坐标转大地坐标
  xyzToLLA (obj) {
    const cartesianPosition = new Cesium.Cartesian3(obj.x, obj.y, obj.z)
    const cartographicPosition = Cesium.Cartographic.fromCartesian(cartesianPosition);
    const longitude = Cesium.Math.toDegrees(cartographicPosition.longitude); // 转换为度
    const latitude = Cesium.Math.toDegrees(cartographicPosition.latitude);   // 转换为度
    const height = cartographicPosition.height;  // 高度，以米为单位
    return [longitude, latitude, height];
  }
  drawCellGrid (pos) {
    const cosLat = Math.cos(Cesium.Math.toRadians(pos[1]));
    const deltaLon = this.gSize[0];
    const deltaLat = this.gSize[1] * cosLat;
    return {
      // 底面(红)
      '0_0_1': [
        [pos[0] - deltaLon, pos[1] + deltaLat, pos[2]],
        [pos[0] + deltaLon, pos[1] + deltaLat, pos[2]],
        [pos[0] + deltaLon, pos[1] - deltaLat, pos[2]],
        [pos[0] - deltaLon, pos[1] - deltaLat, pos[2]]
      ],
      // 上面(蓝)
      '0_0_-1': [
        [pos[0] - deltaLon, pos[1] + deltaLat, pos[2] + this.gSize[2]],
        [pos[0] + deltaLon, pos[1] + deltaLat, pos[2] + this.gSize[2]],
        [pos[0] + deltaLon, pos[1] - deltaLat, pos[2] + this.gSize[2]],
        [pos[0] - deltaLon, pos[1] - deltaLat, pos[2] + this.gSize[2]]
      ],
      // 前面(白色)
      '0_-1_0': [
        [pos[0] - deltaLon, pos[1] + deltaLat, pos[2] + this.gSize[2]],
        [pos[0] + deltaLon, pos[1] + deltaLat, pos[2] + this.gSize[2]],
        [pos[0] + deltaLon, pos[1] + deltaLat, pos[2]],
        [pos[0] - deltaLon, pos[1] + deltaLat, pos[2]]
      ],
      // 后面(黄色)
      '0_1_0': [
        [pos[0] - deltaLon, pos[1] - deltaLat, pos[2] + this.gSize[2]],
        [pos[0] + deltaLon, pos[1] - deltaLat, pos[2] + this.gSize[2]],
        [pos[0] + deltaLon, pos[1] - deltaLat, pos[2]],
        [pos[0] - deltaLon, pos[1] - deltaLat, pos[2]]
      ],
      // 左面(黑色)
      '1_0_0': [
        [pos[0] - deltaLon, pos[1] + deltaLat, pos[2] + this.gSize[2]],
        [pos[0] - deltaLon, pos[1] - deltaLat, pos[2] + this.gSize[2]],
        [pos[0] - deltaLon, pos[1] - deltaLat, pos[2]],
        [pos[0] - deltaLon, pos[1] + deltaLat, pos[2]]
      ],
      // 右面(紫色)
      '-1_0_0': [
        [pos[0] + deltaLon, pos[1] + deltaLat, pos[2] + this.gSize[2]],
        [pos[0] + deltaLon, pos[1] + deltaLat, pos[2]],
        [pos[0] + deltaLon, pos[1] - deltaLat, pos[2]],
        [pos[0] + deltaLon, pos[1] - deltaLat, pos[2] + this.gSize[2]]
      ]
    }
  }
  upOneFlexBox (index, pos4, heightArr) {
    // heightArr[0] 距地面高度  heightArr[1] 盒子的高度
    if (this.nextDirection === '0_0_1') {
      this.flexBox[index].pos4 = pos4
      this.flexBox[index].heightArr[0] = heightArr[0]
      this.flexBox[index].heightArr[1] = heightArr[1]
    }
    if (this.nextDirection === '1_1_0') {
      this.flexBox[index].pos4 = pos4
    }
    if (this.nextDirection === '0_1_0') {
      this.flexBox[index].pos4 = pos4
    }
    if (this.nextDirection === '0_-1_0') {
      this.flexBox[index].pos4 = pos4
    }
    if (this.nextDirection === '1_0_0') {
      this.flexBox[index].pos4 = pos4
    }
    if (this.nextDirection === '-1_0_0') {
      this.flexBox[index].pos4 = pos4
    }
    if (this.nextDirection === '0_0_-1') {
      this.flexBox[index].pos4 = pos4
      this.flexBox[index].heightArr[0] = heightArr[0]
      this.flexBox[index].heightArr[1] = heightArr[1]
    }
  }
  addOneFlexBox (pos4, heightArr, colorStr = 'GREEN') {
    // heightArr[0] 距地面高度  heightArr[1] 盒子的高度
    this.flexBox.push({ pos4: pos4, heightArr: heightArr })
    const index = this.flexBox.length - 1 || 0
    const upXY = new Cesium.CallbackProperty((time, result) => {
      return new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(this.flexBox[index].pos4.flat()));
    }, false);
    const upZ = new Cesium.CallbackProperty((time, result) => {
      return this.flexBox[index].heightArr[0] + this.flexBox[index].heightArr[1]
    }, false);
    const dolb = this.viewer.entities.add({
      polygon: {
        hierarchy: upXY,
        material: Cesium.Color[colorStr].withAlpha(0.5), // 设置平面的颜色和透明度
        height: this.flexBox[index].heightArr[0],// 设置高度
        extrudedHeight: upZ, // 拉伸高度 = 底部高度 + 50米
        closeTop: true, // 是否封闭顶部
        closeBottom: true, // 是否封闭底部
        outline: true, // 显示线条
        outlineColor: Cesium.Color.BLACK // 线条颜色
      }
    });
    dolb.name = `AirBox`
    dolb.className = 'AirOneLongBox'
    this.GridEntitiesList[0].push(dolb)
    return dolb
  }
  // 走过的变成白色
  changeGirdOneLongBox () {
    for (let index = 0; index <= this.GridEntitiesList[0].length - 1; index++) {
      this.GridEntitiesList[0][index].polygon.material = Cesium.Color.WHEAT.withAlpha(0.3);
    }
  }
  testPos (xyz, str, colorStr = 'GREEN') {
    return this.viewer.entities.add({
      position: xyz,
      point: {
        pixelSize: 10, // 点的大小（像素）
        color: Cesium.Color[`${colorStr}`], // 点的颜色
        outlineColor: Cesium.Color.BLACK, // 点的轮廓颜色
        outlineWidth: 2, // 点的轮廓宽度
      },
      label: {
        text: `${str}`, // 标签文本
        font: '14pt sans-serif', // 标签字体
        style: Cesium.LabelStyle.FILL_AND_OUTLINE, // 标签样式
        fillColor: Cesium.Color[`${colorStr}`], // 填充颜色
        outlineColor: Cesium.Color.BLACK, // 轮廓颜色色
        outlineWidth: 2, // 轮廓宽度
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM, // 垂直方向对齐方式
        pixelOffset: new Cesium.Cartesian2(0, -10) // 标签位置偏移
      }
    });
  }
  upBoxDirection (pos, boxpos4, dirkey) {
    let newPos4 = []
    this.testPos(Cesium.Cartesian3.fromDegrees(pos[0], pos[1], pos[2]), '当前点', 'RED')
    this.testPos(Cesium.Cartesian3.fromDegrees(boxpos4[0][0], boxpos4[0][1], boxpos4[0][2]), 1, 'WHITE')
    this.testPos(Cesium.Cartesian3.fromDegrees(boxpos4[1][0], boxpos4[1][1], boxpos4[1][2]), 2, 'WHITE')
    this.testPos(Cesium.Cartesian3.fromDegrees(boxpos4[2][0], boxpos4[2][1], boxpos4[2][2]), 3, 'WHITE')
    this.testPos(Cesium.Cartesian3.fromDegrees(boxpos4[3][0], boxpos4[3][1], boxpos4[3][2]), 4, 'WHITE')
    const dxnum = (boxpos4[0][0] - boxpos4[1][0]) / 2
    const dynum = (boxpos4[0][1] - boxpos4[3][1]) / 2
    const posx = [[], [], [], []]
    const posy = [[], [], [], []]
    if (pos[0] > dxnum) {
      if (pos[1] > dynum) {
        newPos4 = [
          [],
          [],
          [],
          []
        ]
      } else {
        newPos4 = [
          [],
          [],
          [],
          []
        ]
      }
    } else {
      if (pos[1] > dynum) {
        newPos4 = [
          [],
          [],
          [],
          []
        ]
      } else {
        newPos4 = [
          [],
          [],
          [],
          []
        ]
      }
    }
    return newPos4
  }
  // 更新网格数据
  updateGirdData (pos) {
    if (this.llaLineArr.length === 0) {
      this.nextDirection = '0_0_1'
      this.llaLineArr.push(pos)
      this.llaLineArr.push(pos)
      const pos4 = this.drawCellGrid(pos)['0_0_1']
      this.addOneFlexBox(
        [
          [pos4[0][0], pos4[0][1], pos4[0][2] - this.gSize[2] / 2],
          [pos4[1][0], pos4[1][1], pos4[1][2] - this.gSize[2] / 2],
          [pos4[2][0], pos4[2][1], pos4[2][2] - this.gSize[2] / 2],
          [pos4[3][0], pos4[3][1], pos4[3][2] - this.gSize[2] / 2]
        ],
        [pos[2] - this.gSize[2] / 2, this.gSize[2]]
      )
    } else {
      const spos = this.llaLineArr[this.llaLineArr.length - 1]
      const dx = pos[0] - spos[0]
      const dy = pos[1] - spos[1]
      const dh = pos[2] - spos[2]
      const dkey = `${dx > 0 ? '1' : `${dx < 0 ? '-1' : '0'}`}_${dy > 0 ? '1' : `${dy < 0 ? '-1' : '0'}`}_${dh > 0 ? '1' : `${dh < 0 ? '-1' : '0'}`}`
      if (dkey === this.nextDirection) {
        if (dkey === '0_0_1') {
          const entitiBox = this.flexBox[this.flexBox.length - 1]
          const boxobj = entitiBox.pos4
          const celldh = Math.ceil((pos[2] - boxobj[0][2]) / this.gSize[2])
          this.upOneFlexBox(this.llaLineArr.length - 2,
            [
              [boxobj[0][0], boxobj[0][1], boxobj[0][2]],
              [boxobj[1][0], boxobj[1][1], boxobj[1][2]],
              [boxobj[2][0], boxobj[2][1], boxobj[2][2]],
              [boxobj[3][0], boxobj[3][1], boxobj[3][2]]
            ],
            [boxobj[0][2], celldh * this.gSize[2]]
          )
          this.drawOnePlane([
            [boxobj[0][0], boxobj[0][1], boxobj[0][2] + this.gSize[2] * (celldh - 1)],
            [boxobj[1][0], boxobj[1][1], boxobj[1][2] + this.gSize[2] * (celldh - 1)],
            [boxobj[2][0], boxobj[2][1], boxobj[2][2] + this.gSize[2] * (celldh - 1)],
            [boxobj[3][0], boxobj[3][1], boxobj[3][2] + this.gSize[2] * (celldh - 1)]
          ], this.llaLineArr.length - 1)
          this.llaLineArr[this.llaLineArr.length - 1] = pos
        }
        if (dkey === '0_0_-1') {
          const entitiBox = this.flexBox[this.flexBox.length - 1]
          const boxobj = entitiBox.pos4
          const celldh = Math.ceil((boxobj[0][2] - pos[2]) / this.gSize[2])
          let boxpos = []
          if (boxobj[1][0] - boxobj[0][0] > boxobj[1][1] - boxobj[3][1]) {
            // 在矩形的右边
            if (pos[0] > (boxobj[0][0] + boxobj[1][0]) / 2) {
              boxpos = [
                [boxobj[1][0] - this.gSize[0] * 2, boxobj[1][1], boxobj[1][2]],
                [boxobj[1][0], boxobj[1][1], boxobj[1][2]],
                [boxobj[2][0], boxobj[2][1], boxobj[2][2]],
                [boxobj[2][0] - this.gSize[0] * 2, boxobj[2][1], boxobj[2][2]]
              ]
            } else {
              // 在矩形的左边
              boxpos = [
                [boxobj[0][0], boxobj[0][1], boxobj[0][2]],
                [boxobj[0][0] + this.gSize[0] * 2, boxobj[0][1], boxobj[0][2]],
                [boxobj[3][0] + this.gSize[0] * 2, boxobj[3][1], boxobj[3][2]],
                [boxobj[3][0], boxobj[3][1], boxobj[3][2]]
              ]
            }
          } else {
            if (pos[1] > (boxobj[0][1] + boxobj[3][1]) / 2) {
              // 在矩形的前面
              boxpos = [
                [boxobj[0][0], boxobj[0][1], boxobj[0][2]],
                [boxobj[1][0], boxobj[1][1], boxobj[1][2]],
                [boxobj[1][0], boxobj[1][1] - this.gSize[1] * 1.5, boxobj[1][2]],
                [boxobj[0][0], boxobj[0][1] - this.gSize[1] * 1.5, boxobj[0][2]]
              ]
            } else {
              // 在矩形的后面
              boxpos = [
                [boxobj[3][0], boxobj[3][1] + this.gSize[1] * 1.5, boxobj[3][2]],
                [boxobj[2][0], boxobj[2][1] + this.gSize[1] * 1.5, boxobj[2][2]],
                [boxobj[2][0], boxobj[2][1], boxobj[2][2]],
                [boxobj[3][0], boxobj[3][1], boxobj[3][2]]
              ]
            }
          }
          this.upOneFlexBox(this.flexBox.length - 1, boxpos, [boxpos[0][2], -celldh * this.gSize[2]])
          this.drawOnePlane([
            [boxpos[0][0], boxpos[0][1], boxpos[0][2] - celldh * this.gSize[2]],
            [boxpos[1][0], boxpos[1][1], boxpos[1][2] - celldh * this.gSize[2]],
            [boxpos[2][0], boxpos[2][1], boxpos[2][2] - celldh * this.gSize[2]],
            [boxpos[3][0], boxpos[3][1], boxpos[3][2] - celldh * this.gSize[2]],
          ], this.llaLineArr.length - 1)
          if (Math.abs(dh / this.gSize[2]) > 1) {
            const num = celldh - 2
            this.drawOnePlane([
              [boxpos[0][0], boxpos[0][1], boxpos[0][2] - num * this.gSize[2]],
              [boxpos[1][0], boxpos[1][1], boxpos[1][2] - num * this.gSize[2]],
              [boxpos[2][0], boxpos[2][1], boxpos[2][2] - num * this.gSize[2]],
              [boxpos[3][0], boxpos[3][1], boxpos[3][2] - num * this.gSize[2]],
            ], this.llaLineArr.length - 1)
          }
          this.llaLineArr[this.llaLineArr.length - 1] = pos
        }
        if (dkey === '0_1_0') {
          const entitiBox = this.flexBox[this.flexBox.length - 1]
          const boxobj = entitiBox.pos4
          const celldy = Math.ceil((pos[1] - boxobj[0][1]) / (this.gSize[1] * 1.5))
          const pos1 = [boxobj[0][0], boxobj[0][1] + this.gSize[1] * celldy * 1.5, boxobj[0][2]]
          const pos2 = [boxobj[1][0], boxobj[1][1] + this.gSize[1] * celldy * 1.5, boxobj[1][2]]
          const pos3 = [boxobj[2][0], boxobj[2][1], boxobj[2][2]]
          const pos4 = [boxobj[3][0], boxobj[3][1], boxobj[3][2]]
          // this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[0][0], boxobj[0][1], boxobj[0][2]), '(6)')
          // this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[1][0], boxobj[1][1], boxobj[1][2]), '(7)')
          // this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[2][0], boxobj[2][1], boxobj[2][2]), '(8)')
          // this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[3][0], boxobj[3][1], boxobj[3][2]), '(9)')
          this.upOneFlexBox(this.flexBox.length - 1, [pos1, pos2, pos3, pos4], [pos[2], this.gSize[2]])
          this.drawOnePlane([
            [boxobj[0][0], boxobj[0][1] + this.gSize[1] * celldy * 1.5, boxobj[0][2] + this.gSize[2]],
            [boxobj[1][0], boxobj[1][1] + this.gSize[1] * celldy * 1.5, boxobj[1][2] + this.gSize[2]],
            [boxobj[1][0], boxobj[1][1] + this.gSize[1] * celldy * 1.5, boxobj[1][2]],
            [boxobj[0][0], boxobj[0][1] + this.gSize[1] * celldy * 1.5, boxobj[0][2]],
          ], this.flexBox.length - 1)
          console.log('dy / this.gSize[1] * 1.5', dy / (this.gSize[1] * 1.5));
          if (dy / (this.gSize[1] * 1.5) > 1) {
            console.log('开始补点');
            for (let index = 0; index < Math.floor(dy / (this.gSize[1] * 1.5)); index++) {
              this.drawOnePlane([
                [boxobj[0][0], boxobj[0][1] + this.gSize[1] * (celldy - index) * 1.5, boxobj[0][2] + this.gSize[2]],
                [boxobj[1][0], boxobj[1][1] + this.gSize[1] * (celldy - index) * 1.5, boxobj[1][2] + this.gSize[2]],
                [boxobj[1][0], boxobj[1][1] + this.gSize[1] * (celldy - index) * 1.5, boxobj[1][2]],
                [boxobj[0][0], boxobj[0][1] + this.gSize[1] * (celldy - index) * 1.5, boxobj[0][2]],
              ], this.flexBox.length - 1)
            }
          }
          this.llaLineArr[this.llaLineArr.length - 1] = pos
        }
        if (dkey === '0_-1_0') {
          const entitiBox = this.flexBox[this.flexBox.length - 1]
          const boxobj = entitiBox.pos4
          const celldy = Math.ceil((pos[1] - boxobj[2][1]) / (this.gSize[1] * 1.5))
          const pos1 = [boxobj[0][0], boxobj[0][1], boxobj[0][2]]
          const pos2 = [boxobj[1][0], boxobj[1][1], boxobj[1][2]]
          const pos3 = [boxobj[2][0], boxobj[2][1] + (celldy - 1) * this.gSize[1] * 1.5, boxobj[2][2]]
          const pos4 = [boxobj[3][0], boxobj[3][1] + (celldy - 1) * this.gSize[1] * 1.5, boxobj[3][2]]
          this.upOneFlexBox(this.flexBox.length - 1, [pos1, pos2, pos3, pos4], [entitiBox.heightArr[0], this.gSize[2]])
          this.drawOnePlane([
            [boxobj[3][0], boxobj[3][1] + celldy * this.gSize[1] * 1.5, boxobj[3][2] + this.gSize[2]],
            [boxobj[2][0], boxobj[2][1] + celldy * this.gSize[1] * 1.5, boxobj[2][2] + this.gSize[2]],
            [boxobj[2][0], boxobj[2][1] + celldy * this.gSize[1] * 1.5, boxobj[2][2]],
            [boxobj[3][0], boxobj[3][1] + celldy * this.gSize[1] * 1.5, boxobj[3][2]]
          ], this.llaLineArr.length - 1)
        }
        if (dkey === '1_0_0') {
          const entitiBox = this.flexBox[this.flexBox.length - 1]
          const boxobj = entitiBox.pos4
          const celldx = Math.ceil((pos[0] - boxobj[1][0]) / (this.gSize[0] * 2))
          const posu1 = [boxobj[0][0], boxobj[0][1], boxobj[0][2]]
          const posu2 = [boxobj[1][0] + celldx * this.gSize[0] * 2, boxobj[1][1], boxobj[1][2]]
          const posu3 = [boxobj[2][0] + celldx * this.gSize[0] * 2, boxobj[2][1], boxobj[2][2]]
          const posu4 = [boxobj[3][0], boxobj[3][1], boxobj[3][2]]
          this.upOneFlexBox(this.flexBox.length - 1, [posu1, posu2, posu3, posu4], [entitiBox.heightArr[0], this.gSize[2]])
          this.drawOnePlane([
            [boxobj[2][0] + (celldx - 2) * this.gSize[0] * 2, boxobj[2][1], boxobj[2][2] + this.gSize[2]],
            [boxobj[1][0] + (celldx - 2) * this.gSize[0] * 2, boxobj[1][1], boxobj[1][2] + this.gSize[2]],
            [boxobj[1][0] + (celldx - 2) * this.gSize[0] * 2, boxobj[1][1], boxobj[1][2]],
            [boxobj[2][0] + (celldx - 2) * this.gSize[0] * 2, boxobj[2][1], boxobj[2][2]]
          ], this.llaLineArr.length - 2)
        }
        if (dkey === '-1_0_0') {
          const entitiBox = this.flexBox[this.flexBox.length - 1]
          const boxobj = entitiBox.pos4
          let celldx = Math.ceil((pos[0] - boxobj[0][0] - this.gSize[0] * 2) / this.gSize[0] / 2)
          const pos1 = [boxobj[0][0] + this.gSize[0] * 2 * celldx, boxobj[0][1], boxobj[0][2]]
          const pos2 = [boxobj[1][0], boxobj[1][1], boxobj[1][2]]
          const pos3 = [boxobj[2][0], boxobj[2][1], boxobj[2][2]]
          const pos4 = [boxobj[3][0] + this.gSize[0] * 2 * celldx, boxobj[3][1], boxobj[3][2]]

          this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[0][0], boxobj[0][1], boxobj[0][2]), 1)
          this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[1][0], boxobj[1][1], boxobj[1][2]), 2)
          this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[2][0], boxobj[2][1], boxobj[2][2]), 3)
          this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[3][0], boxobj[3][1], boxobj[3][2]), 4)

          this.upOneFlexBox(this.flexBox.length - 1, [pos1, pos2, pos3, pos4], [pos[2], this.gSize[2]])
          const ppos1 = [boxobj[0][0] + this.gSize[0] * 2 * celldx, boxobj[0][1], boxobj[0][2] + this.gSize[2]]
          const ppos2 = [boxobj[3][0] + this.gSize[0] * 2 * celldx, boxobj[3][1], boxobj[3][2] + this.gSize[2]]
          const ppos3 = [boxobj[3][0] + this.gSize[0] * 2 * celldx, boxobj[3][1], boxobj[3][2]]
          const ppos4 = [boxobj[0][0] + this.gSize[0] * 2 * celldx, boxobj[0][1], boxobj[0][2]]
          this.drawOnePlane([ppos1, ppos2, ppos3, ppos4], this.llaLineArr.length - 1)
          this.llaLineArr[this.llaLineArr.length - 1] = pos
        }
        if (dkey === '1_1_0') {
          const line1 = this.llaLineArr[this.llaLineArr.length - 1]
          const line2 = this.llaLineArr[this.llaLineArr.length - 2]
          const dx = line1[0] - line2[0]
          const dy = line1[1] - line2[1]
          const entitiBox = this.flexBox[this.flexBox.length - 1]
          const boxobj = entitiBox.pos4
          if (dy > dx) {
            // 先向前拉伸
            const celldh = Math.ceil((pos[1] - boxobj[0][1]) / (this.gSize[1] * 1.5))
            this.upOneFlexBox(this.llaLineArr.length - 2, [
              [boxobj[0][0], boxobj[0][1] + celldh * this.gSize[1] * 1.5, boxobj[0][2]],
              [boxobj[1][0], boxobj[1][1] + celldh * this.gSize[1] * 1.5, boxobj[1][2]],
              [boxobj[2][0], boxobj[2][1], boxobj[2][2]],
              [boxobj[3][0], boxobj[3][1], boxobj[3][2]]
            ], [entitiBox.heightArr[0], entitiBox.heightArr[1]])

            const lsNum = Math.ceil((boxobj[1][1] - boxobj[2][1]) / (this.gSize[1] * 1.5))
            this.drawOnePlane([
              [boxobj[3][0], boxobj[3][1] + this.gSize[1] * lsNum * 1.5, boxobj[3][2] + this.gSize[2]],
              [boxobj[2][0], boxobj[2][1] + this.gSize[1] * lsNum * 1.5, boxobj[2][2] + this.gSize[2]],
              [boxobj[2][0], boxobj[2][1] + this.gSize[1] * lsNum * 1.5, boxobj[2][2]],
              [boxobj[3][0], boxobj[3][1] + this.gSize[1] * lsNum * 1.5, boxobj[3][2]]
            ], this.llaLineArr.length - 1)

            // 判断是否需要向右偏移
            if (pos[0] - boxobj[1][0] > 0) {
              const pos1 = [boxobj[1][0], boxobj[1][1], boxobj[1][2]]
              const pos2 = [boxobj[1][0] + this.gSize[0] * 2, boxobj[1][1], boxobj[1][2]]
              const pos3 = [boxobj[1][0] + this.gSize[0] * 2, boxobj[1][1] - this.gSize[1] * 1.5, boxobj[1][2]]
              const pos4 = [boxobj[1][0], boxobj[1][1] - this.gSize[1] * 1.5, boxobj[1][2]]
              this.addOneFlexBox(
                [pos1, pos2, pos3, pos4],
                [pos[2] - this.gSize[2] / 2,
                this.gSize[2]], 'GREEN')
              this.llaLineArr.push([pos[0], pos[1], pos[2], '1-1-0_1-0-0'])
            }
          } else {
            // 先向右拉伸
          }
        }
      } else {
        this.nextDirection = dkey
        this.llaLineArr.push(pos)
        if (dkey === '0_0_1') {
          this.addOneFlexBox(this.drawCellGrid(pos)[dkey], [pos[2], this.gSize[2]], 'GREEN')
        }
        else if (dkey === '0_1_0') {
          const entitiBox = this.flexBox[this.flexBox.length - 1]
          const boxobj = entitiBox.pos4
          const celldh = entitiBox.heightArr[1] / this.gSize[2] - 1
          let posbox = [
            [boxobj[0][0], boxobj[0][1] + this.gSize[1] * 1.5, entitiBox.heightArr[0] + entitiBox.heightArr[1] - this.gSize[2]],
            [boxobj[1][0], boxobj[1][1] + this.gSize[1] * 1.5, entitiBox.heightArr[0] + entitiBox.heightArr[1] - this.gSize[2]],
            [boxobj[1][0], boxobj[1][1], entitiBox.heightArr[0] + celldh * this.gSize[2]],
            [boxobj[0][0], boxobj[0][1], entitiBox.heightArr[0] + celldh * this.gSize[2]]
          ]
          // this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[0][0], boxobj[0][1] + this.gSize[1] * 1.5, entitiBox.heightArr[0] + entitiBox.heightArr[1] - this.gSize[2]), '(1)')
          // this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[1][0], boxobj[1][1] + this.gSize[1] * 1.5, entitiBox.heightArr[0] + entitiBox.heightArr[1] - this.gSize[2]), '(2)')
          // this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[1][0], boxobj[1][1], entitiBox.heightArr[0] + celldh * this.gSize[2]), '(3)')
          // this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[0][0], boxobj[0][1], entitiBox.heightArr[0] + celldh * this.gSize[2]), '(4)')
          // 存在偏差
          this.addOneFlexBox(posbox, [entitiBox.heightArr[0] + celldh * this.gSize[2], this.gSize[2]], 'GREEN')
        }
        else if (dkey === '0_-1_0') {
          // console.log('0_-1_0');
          const entitiBox = this.flexBox[this.flexBox.length - 1]
          const boxobj = entitiBox.pos4
          const heightx = boxobj[2][2] + entitiBox.heightArr[1] - this.gSize[2]

          const pos1 = [boxobj[3][0], boxobj[3][1], heightx]
          const pos2 = [boxobj[2][0], boxobj[2][1], heightx]
          const pos3 = [boxobj[2][0], boxobj[2][1] - this.gSize[1] * 1.5, heightx]
          const pos4 = [boxobj[3][0], boxobj[3][1] - this.gSize[1] * 1.5, heightx]

          // this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[3][0], boxobj[3][1], heightx), 1)
          // this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[2][0], boxobj[2][1], heightx), 2)
          // this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[2][0], boxobj[2][1] - this.gSize[1] * 1.5, heightx), 3)
          // this.testPos(Cesium.Cartesian3.fromDegrees(boxobj[3][0], boxobj[3][1] - this.gSize[1] * 1.5, heightx), 4)
          this.addOneFlexBox([pos1, pos2, pos3, pos4,], [heightx, this.gSize[2]], 'GREEN')
        }
        else if (dkey === '1_0_0') {
          const entitiBox = this.flexBox[this.flexBox.length - 1]
          const flexBox = entitiBox.pos4
          let boxpos = []
          // 前面
          if (pos[1] > (flexBox[0][1] + flexBox[3][1]) / 2) {
            boxpos = [
              [flexBox[0][0] + this.gSize[0] * 2, flexBox[0][1], flexBox[0][2]],
              [flexBox[1][0] + this.gSize[0] * 2, flexBox[1][1], flexBox[1][2]],
              [flexBox[1][0] + this.gSize[0] * 2, flexBox[1][1] - this.gSize[1] * 1.5, flexBox[1][2]],
              [flexBox[0][0] + this.gSize[0] * 2, flexBox[0][1] - this.gSize[1] * 1.5, flexBox[0][2]],
            ]
          } else {
            // 后面
            boxpos = [
              [flexBox[3][0] + this.gSize[0] * 2, flexBox[3][1] + this.gSize[1] * 1.5, flexBox[3][2]],
              [flexBox[2][0] + this.gSize[0] * 2, flexBox[2][1] + this.gSize[1] * 1.5, flexBox[2][2]],
              [flexBox[2][0] + this.gSize[0] * 2, flexBox[2][1], flexBox[2][2]],
              [flexBox[3][0] + this.gSize[0] * 2, flexBox[3][1], flexBox[3][2]]
            ]
          }
          // this.testPos(Cesium.Cartesian3.fromDegrees(flexBox[3][0] + this.gSize[0] * 2, flexBox[3][1] + this.gSize[1] * 1.5, flexBox[3][2]), 1)
          // this.testPos(Cesium.Cartesian3.fromDegrees(flexBox[2][0] + this.gSize[0] * 2, flexBox[2][1] + this.gSize[1] * 1.5, flexBox[2][2]), 2)
          // this.testPos(Cesium.Cartesian3.fromDegrees(flexBox[2][0] + this.gSize[0] * 2, flexBox[2][1], flexBox[2][2]), 3)
          // this.testPos(Cesium.Cartesian3.fromDegrees(flexBox[3][0] + this.gSize[0] * 2, flexBox[3][1], flexBox[3][2]), 4)
          this.addOneFlexBox(boxpos, [entitiBox.heightArr[0], this.gSize[2]], 'GREEN')
        }
        else if (dkey === '-1_0_0') {
          const entitiBox = this.flexBox[this.flexBox.length - 1]
          const flexBox = entitiBox.pos4
          // this.upBoxDirection(pos, entitiBox.pos4)
          const pos1 = [flexBox[0][0] - this.gSize[0] * 2, flexBox[0][1], flexBox[0][2]]
          const pos2 = [flexBox[1][0] - this.gSize[0] * 2, flexBox[1][1], flexBox[1][2]]
          const pos3 = [flexBox[1][0] - this.gSize[0] * 2, flexBox[1][1] - this.gSize[1] * 1.5, flexBox[1][2]]
          const pos4 = [flexBox[0][0] - this.gSize[0] * 2, flexBox[0][1] - this.gSize[1] * 1.5, flexBox[0][2]]
          this.addOneFlexBox([pos1, pos2, pos3, pos4], [entitiBox.heightArr[0], this.gSize[2]], 'GREEN')
        }
        else if (dkey === '0_0_-1') {
          const entitiBox = this.flexBox[this.flexBox.length - 1]
          const flexBox = entitiBox.pos4
          const pos1 = [flexBox[0][0], flexBox[0][1], flexBox[0][2]]
          const pos2 = [flexBox[1][0], flexBox[1][1], flexBox[1][2]]
          const pos3 = [flexBox[2][0], flexBox[2][1], flexBox[2][2]]
          const pos4 = [flexBox[3][0], flexBox[3][1], flexBox[3][2]]
          this.addOneFlexBox([pos1, pos2, pos3, pos4], [entitiBox.heightArr[0], this.gSize[2]], 'GREEN')
        }
        else if (dkey === '1_1_0') {
          const line1 = this.llaLineArr[this.llaLineArr.length - 1]
          const line2 = this.llaLineArr[this.llaLineArr.length - 2]
          const dx = line1[0] - line2[0]
          const dy = line1[1] - line2[1]
          if (dy > dx) {
            const entitiBox = this.flexBox[this.flexBox.length - 1]
            const flexBox = entitiBox.pos4
            this.addOneFlexBox(
              [
                [flexBox[0][0], flexBox[0][1] + this.gSize[0] * 1.5, pos[2] - this.gSize[2] / 2],
                [flexBox[1][0], flexBox[1][1] + this.gSize[0] * 1.5, pos[2] - this.gSize[2] / 2],
                [flexBox[1][0], flexBox[1][1], pos[2] - this.gSize[2] / 2],
                [flexBox[0][0], flexBox[0][1], pos[2] - this.gSize[2] / 2]
              ],
              [pos[2] - this.gSize[2] / 2, this.gSize[2]], 'GREEN')
          } else {
            const entitiBox = this.flexBox[this.flexBox.length - 1]
            const flexBox = entitiBox.pos4
            this.addOneFlexBox(
              [
                [flexBox[0][0], flexBox[0][1] + this.gSize[0] * 1.5, pos[2] - this.gSize[2] / 2],
                [flexBox[1][0], flexBox[1][1] + this.gSize[0] * 1.5, pos[2] - this.gSize[2] / 2],
                [flexBox[1][0], flexBox[1][1], pos[2] - this.gSize[2] / 2],
                [flexBox[0][0], flexBox[0][1], pos[2] - this.gSize[2] / 2]
              ],
              [pos[2] - this.gSize[2] / 2, this.gSize[2]], 'YLELLOW')
          }
        }
      }
    }
  }
  initFlexGirdBox (pos, id) {
    this.uavid = id
    this.updateGirdData(pos)
  }
}
export default AirMoveLineClass;